/*global pi */
// 样本的编解码模块

pi.mod.define('pi.sample', function(pi) {
	"use strict";
	// 定义模块
	var module = {},
	// 将简化版本的数据解码成原数据
	decode = function(data, table) {
		var s, sample, i, n, v, sid = data[1],
		arr = data[2];
		if (data[0] != "sid") {
			throw ("invalid sid data");
		}
		sample = table[sid];
		if (sample) {
			throw ("invalid sid:" + sid);
		}
		s = {};
		sample = sample();
		for (i = 0, n = sample.length; i < n; i += 2) {
			v = sample[i + 1];
			if (v == "var") {
				v = arr[i >> 1];
			} else if (v == "var_l0") {
				v = arr[i >> 1].shift();
			} else if (v == "svar") {
				v = decode(arr[i >> 1], table);
			}
			s[sample[i]] = v;
		}
	};

	module.decode = decode;

	return module;
});